/*
 * ContecxtOperation.java
 *
 * Created on 2008-2-26, PM 5:48
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package orest.common.util;

import orest.common.valueobject.UserSessionInfo;
import java.io.*;
import javax.servlet.*;
import java.util.Vector;
import java.util.Hashtable;
import javax.servlet.http.*;
import orest.common.valueobject.UserSessionInfo;

/**
 * This class provide the opration about tomcat context, for example
 * store session value into context, get session value from context by cookie.
 * This class is define for share the session data between the nast.org.cn and 
 * tech110.cn
 * 
 * @author yan dongdong
 */
public class ContecxtOperation {
    
    /** Creates a new instance of ContecxtOperation */
    public ContecxtOperation() {
    }
    
    /**
     * Date: 2008/02/26
     * Input: context    used it to stord sessionid into contex node
     *        sessionid  session key
     *        sessionInfo  session object value
     * output: true/false
     * Description: Store the session infomation into context.
     */
    public synchronized static boolean storeDataInContext
            (ServletContext context, String sessionid, UserSessionInfo sessionInfo) {
        try{
            // load the shared_userroles from the context
            Hashtable shareddata = (Hashtable)context.getAttribute("shared_session");

            // if not yet available, create a new one
            if (shareddata==null) {
                shareddata = new Hashtable();
            }

            // store the session object of the current session
            shareddata.put(sessionid, sessionInfo);

            // store the shareddata back into the Context
            context.setAttribute("shared_session", shareddata);
            return true;
        }
        catch(Exception e) {
            e.printStackTrace();
            System.out.println("The error occured at ContecxtOperation's " +
                                " storeDataInContext");
            return false;
        }
    }
    
    /**
     * Date: 2008/02/27
     * Input: context    used it to reference the context content and get session value
     *        ssosessionid  session key
     * output: UserSessionInfo session value
     * Description: get the session infomation from context.
     */
    public static UserSessionInfo getSessionValueFromContext(ServletContext context, 
                                                               String ssosessionid) {
        
        // read the name of the Context storing shared session data
        String SignonContext = context.getInitParameter("SharedSessiondataContext");
        
        // get the Context containing the shared session data
        ServletContext ssocontext = context.getContext(SignonContext);
        
        UserSessionInfo sessionInfo = null;
        
        if(ssocontext == null) {
            System.out.println("When getSessionValueFromContext, the context is null!");
            System.out.println("Context Parameter Name: SharedSessiondataContext");
            System.out.println("Context Parameter Value: " + SignonContext);
        }
        else {
            // read the shareddata
            Hashtable shareddata = (Hashtable)ssocontext.getAttribute("shared_session");
            if (shareddata!=null) {
                // get the right Object using the ssosessionid
                sessionInfo = (UserSessionInfo)shareddata.get(ssosessionid);
            }
        }
        return sessionInfo;
    }




    
}
